package com.taotao.service.ipml;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TreeNode;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.pojo.TbItemCatExample.Criteria;
import com.taotao.service.IItemCatService;

/**
 * 类目业务层
 * 
 * @author Administrator
 *
 */
@Service
public class ItemCatServiceImpl implements IItemCatService {
	@Autowired
	private TbItemCatMapper tbItemCatMapper;

	/**
	 * 获取当前节点id的查询当前结点的子节点
	 */
	@Override
	public List<TreeNode> getItemCatList(Long parentId) {
		TbItemCatExample example = new TbItemCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbItemCat> tbItemCats = this.tbItemCatMapper
				.selectByExample(example);
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		for (TbItemCat tbItemCat : tbItemCats) {
			long id = tbItemCat.getId();
			String text = tbItemCat.getName();
			String state = tbItemCat.getIsParent() ? "closed" : "open";
			System.out.println(state);
			TreeNode treeNode = new TreeNode(id, text, state);
			treeNodes.add(treeNode);
		}
		return treeNodes;

	}

	/**
	 * 通过id查询商品种类
	 */
	@Override
	public TbItemCat getItemByItemCatId(Long itemCatId) {
		// TODO Auto-generated method stub
		if (itemCatId != null) {
			return this.tbItemCatMapper.selectByPrimaryKey(itemCatId);

		}
		return null;
	}
}
